<?php

namespace App\Http\Controllers\Company;

use App\Facades\Style;
use App\Models\Company\CompanyDepartment;
use App\Models\Company\CompanyBasic;
use Illuminate\Http\Request;
use App\Http\Requests\Company\Department\DepartmentCreateRequest;
use App\Http\Requests\Company\Department\DepartmentUpdateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class DepartmentController extends Controller{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request){
        $user = auth()->guard('company')->user();
        $keyword = $request->input('keyword');
        //获取面试通知记录
        $list = DB::table('company_department')
            ->select(
                'id', 'name', 'contacts', 'mobile', 'phone'
            )
            ->where('uid', $user->uid)
            ->where(function($query) use ($keyword){
                $query->where('name', 'like', '%' . $keyword . '%')->orWhere('contacts', 'like', '%' . $keyword . '%');
            })
            ->orderBy('updated_at', 'desc')
            ->paginate(20);

        foreach($list as $key => $v){
            $v->phone = $v->phone ? $v->phone : $v->mobile;
        }

        $input = arrayFilter(
            array('keyword' => $keyword)
        );

        return Style::view('company.departmentIndex', compact('list', 'input'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(){
        return Style::view('company.departmentCreate');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(DepartmentCreateRequest $request){
        $user = auth()->guard('company')->user();
        $input = $request->only('name', 'contacts', 'mobile', 'phone', 'fax', 'email');

        //获取用户信息ID
        $input['uid'] = $user->uid;
        $companyBasic = CompanyBasic::select('id')->where('uid', $user->uid)->orderBy('updatetime', 'desc')->first();
        $input['com_id'] = $companyBasic->id;
        $id = CompanyDepartment::insertGetId($input);

        if($request->ajax()){
            return response()->json(['status' => 200, 'id' => $id, 'name' => $input['name']]);
        }else{
            return redirect('company/department');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id){
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id){
        $user = auth()->guard('company')->user();
        $CompanyDepartment = CompanyDepartment::where('uid', $user->uid)->find($id);
        return Style::view('company.departmentEdit', compact('CompanyDepartment'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(DepartmentUpdateRequest $request, $id){
        $user = auth()->guard('company')->user();
        $input = $request->only('name', 'contacts', 'mobile', 'phone', 'fax', 'email');
        CompanyDepartment::where('id', $id)->where('uid', $user->uid)->update($input);
        return redirect('company/department');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id){
        $user = auth()->guard('company')->user();
        if($id and $id != 0){
            $deletedRows = CompanyDepartment::where('id', $id)->where('uid', $user->uid)->delete();
            if($deletedRows){
                return back()->withInput();
            }
        }elseif($id == 0 and is_array($request->selected_id)){
            $deletedRows = CompanyDepartment::whereIn('id', $request->selected_id)->where('uid', $user->uid)->delete();
            if($deletedRows){
                return back()->withInput();
            }
        }
        //return back()->withInput();
    }
}
